package com.decent.text.pdf.test2;

import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author 王雪杰
 * @date 2021/11/13 10:12
 */
public class CreateAllPdf {
    public static void main(String[] args) throws Exception {
        createAllPdf();
    }

    /**
     * 抠模板
     * todo Adobe Acrobat Pro DC 创建模板
     *
     * @throws Exception
     */
    public static void createAllPdf() throws Exception {
        // 填充创建pdf
        PdfReader reader = null;
        PdfStamper stamp = null;
        try {
            reader = new PdfReader("D:\\idea\\workspace\\decent\\dumas_1\\dumas\\seller\\src\\main\\resources\\transfer-agreement-template.pdf");
            SimpleDateFormat simp = new SimpleDateFormat("dd-HH-mm-ss");
            String times = simp.format(new Date()).trim();
            // 创建生成报告名称
            String root = "D:\\idea\\workspace\\decent\\dumas_1\\dumas\\seller\\src\\main\\resources" + File.separator;
            File deskFile = new File(root, times + ".pdf");
            stamp = new PdfStamper(reader, new FileOutputStream(deskFile));
            // 取出报表模板中的所有字段
            AcroFields form = stamp.getAcroFields();
            // 填充数据
            // form.setField("contractNo", "123412341234asd");
            // form.setField("signImage", "http://dx-pub-test.oss-cn-beijing.aliyuncs.com/nintendo/SIGN/2021-11-17/1343188944411900");

            form.setField("createYear", "2021");
            form.setField("createMonth", "21");
            form.setField("createDay", "91");
            form.setField("Text1", "asdf");
            // // 遍历map装入数据
            // for (Entry<String, String> entry : map.entrySet()) {
            //     form.setField(entry.getKey(), entry.getValue());
            // }
            // 插入图片
            int pageNo = form.getFieldPositions("signImage").get(0).page;
            Rectangle signRect = form.getFieldPositions("signImage").get(0).position;
            float x = signRect.getLeft();
            float y = signRect.getBottom();
            Image image = Image.getInstance("http://dx-pub-test.oss-cn-beijing.aliyuncs.com/nintendo/SIGN/2021-11-17/1343188944411900");
            // 获取操作的页面
            PdfContentByte under = stamp.getOverContent(pageNo);
            // 根据域的大小缩放图片
            image.scaleToFit(signRect.getWidth(), signRect.getHeight());
            // 添加图片
            image.setAbsolutePosition(x, y);
            under.addImage(image);

            // 这里true表示pdf可编辑
            stamp.setFormFlattening(true);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (stamp != null) {
                stamp.close();
            }
            if (reader != null) {
                reader.close();
            }
        }
    }

}
